package handler;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Objects;

import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import bean.Account;
import dao.AccountDAO;
import dao.impl.AccountDAOImpl;
import jdbc.ContactsConnection;
import tool.ObjectConverter;

public class AccountHandler extends HttpServlet {
    private static final long serialVersionUID = 7112379607983808742L;
    private Connection conn;
    private AccountDAO accountDAO;

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        int accountId = ObjectConverter.castObjectToInteger(session.getAttribute("account-id"));
        String accountName = (String) session.getAttribute("account-name");
        boolean onlineState = accountId > 0 && Objects.nonNull(accountName) && accountName.length() > 0;
        String operate = req.getParameter("operate");
        try {
            switch (operate) {
            case "login":
                if (onlineState) {
                    resp.sendRedirect("contact-handler");
                } else {
                    accountName = req.getParameter("account-name");
                    String password = req.getParameter("password");
                    Account temporary = new Account(accountName, password);
                    if (Objects.nonNull(accountName) && accountName.length() > 0) {
                        if (Objects.nonNull(password) && password.length() > 0) {
                            accountId = accountDAO.selectIdByAccount(temporary);
                            if (accountId > 0) {
                                session.setAttribute("account-id", accountId);
                                session.setAttribute("account-name", accountName);
                                resp.sendRedirect("contact-handler");
                            } else {
                                resp.sendRedirect("login");
                            }
                        }
                    }
                }
                break;
            case "add-account":
                if (onlineState) {
                    resp.sendRedirect("contact-handler");
                } else {
                    accountName = req.getParameter("account-name");
                    String password = req.getParameter("password");
                    if (Objects.nonNull(accountName) && accountName.length() > 0) {
                        if (Objects.nonNull(password) && password.length() > 0) {
                            Account temporary = new Account(accountName, password);
                            boolean registerState = accountDAO.insert(temporary);
                            if (registerState) {
                                resp.sendRedirect("login");
                            } else {
                                resp.sendRedirect("register");
                            }
                        }
                    }
                }
                break;
            case "modify-password":
                if (onlineState) {
                    String oldPassword = req.getParameter("old-password");
                    String newPassword = req.getParameter("new-password");
                    Account temporary = new Account(accountId, accountName, oldPassword);
                    // 修改密码成功
                    if (accountDAO.update(temporary, newPassword)) {
                        resp.sendRedirect("logout");
                    } else {
                        // 重定向至修改密码页面
                        resp.sendRedirect("change-password");
                    }
                } else {
                    resp.sendRedirect("login");
                }
                break;
            }
        } catch (NamingException | SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.service(req, resp);
    }

    @Override
    public void destroy() {
        try {
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        super.destroy();
    }

    @Override
    public void init() throws ServletException {
        try {
            conn = ContactsConnection.open();
            accountDAO = new AccountDAOImpl(conn);
        } catch (NamingException | SQLException e) {
            e.printStackTrace();
        }
        super.init();
    }
}
